package com.ly.canvasdemo

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import com.ly.canvasdemo.NavigationBarInfo.getNavigationBarHeight


class CanvasView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0
) : View(context, attributeSet, defStyle) {
    private val paint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }
    private val navBtHeight by lazy {
        context.getNavigationBarHeight() - 50
    }

    private val winWidth by lazy {
        context.resources.displayMetrics.widthPixels
    }

    private val winHeight by lazy {
        context.resources.displayMetrics.heightPixels - navBtHeight
    }
    private val coo by lazy {
        Point(500, 500)
    }

    private val dash by lazy {
        DashPathEffect(floatArrayOf(10f, 5f), 0f)
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawGrid(canvas)
        drawCoo(canvas)
        //将画布的原点移到500,500处
        canvas.translate(500f, 500f)

       // drawPoint(canvas)
       // drawLine(canvas)
       // drawRect(canvas)
        drawLikeCircle(canvas)
    }
    /**************************************************************************************************/

    /**
     * 绘制点
     * @param canvas canvas
     */
    private fun drawPoint(canvas: Canvas) {
        paint.apply {
            color = Color.RED
            strokeWidth = 10f
        }
        canvas.drawPoint(100f, 100f, paint)
        canvas.drawPoints(
            floatArrayOf(
                400f, 400f,
                400f, 100f,
                500f, 500f
            ), paint
        )
    }

    /**
     * 绘制线条
     * @param canvas canvas
     */
    private fun drawLine(canvas: Canvas) {
        paint.apply {
            color = Color.BLUE
            strokeWidth = 10f
        }
        canvas.drawLine(100f, 100f, 400f, 400f, paint)
        canvas.drawLines(
            floatArrayOf(
                100f, 100f, 400f, 100f,
                400f, 100f, 400f, 400f
            ), paint
        )
    }

    /**
     * 绘制矩形
     * @param canvas canvas
     */

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun drawRect(canvas: Canvas){
        paint.apply {
            color = Color.parseColor("#00D7FF")
            strokeWidth = 6f
            style=Paint.Style.STROKE
        }
        canvas.drawRect(50f,50f,450f,450f,paint)
        //等价于 val rect=RectF(50f,50f,450f,450f) canvas.drawRect(rect,paint)
        canvas.drawRoundRect(50f,500f,400f,600f,50f,50f,paint)
        //需要api 21以上的
        //等价于 val rect=RectF(50f,500f,400f,600f) canvas.drawRoundRect(rect,50f,50f,paint)
    }


    /**
     * 绘制类圆
     * @param canvas canvas
     */
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun drawLikeCircle(canvas: Canvas){
        paint.apply {
            color=Color.parseColor("#191919")
            style=Paint.Style.STROKE
        }

        canvas.drawCircle(400f,500f,100f,paint)
        canvas.drawOval(250f,350f,550f,650f,paint)//限于api 21以上
        val rectF=RectF(300f,450f,500f,550f)
        canvas.drawOval(rectF,paint)
    }

    /**************************************************************************************************/
    /**
     * 绘制坐标轴
     * @param canvas canvas
     */
    private fun drawCoo(canvas: Canvas) {
        paint.apply {
            color = Color.BLACK
            strokeWidth = 4f
            style = Paint.Style.STROKE
            pathEffect = null
        }
        val path = Path()
        //x正半轴
        path.moveTo(coo.x.toFloat(), coo.y.toFloat())
        path.lineTo(winWidth.toFloat(), coo.y.toFloat())
        //x负半轴
        path.moveTo(coo.x.toFloat(), coo.y.toFloat())
        path.lineTo((coo.x - winWidth).toFloat(), coo.y.toFloat())

        //y正半轴
        path.moveTo(coo.x.toFloat(), coo.y.toFloat())
        path.lineTo(coo.x.toFloat(), (coo.y - winHeight).toFloat())
        //y负半轴
        path.moveTo(coo.x.toFloat(), coo.y.toFloat())
        path.lineTo(coo.x.toFloat(), winHeight.toFloat())
        canvas.drawPath(path, paint)

        //x轴箭头
        canvas.drawLine(winWidth.toFloat(), coo.y.toFloat(), winWidth - 40f, coo.y + 20f, paint)
        canvas.drawLine(winWidth.toFloat(), coo.y.toFloat(), winWidth - 40f, coo.y - 20f, paint)

        //y轴下箭头
        canvas.drawLine(coo.x.toFloat(), winHeight.toFloat(), coo.x - 20f, winHeight - 40f, paint)
        canvas.drawLine(coo.x.toFloat(), winHeight.toFloat(), coo.x + 20f, winHeight - 40f, paint)

        drawTextCoo(canvas)
    }

    /**
     * 绘制坐标轴上的文字
     * @param canvas canvas
     */
    private fun drawTextCoo(canvas: Canvas) {
        paint.apply {
            textSize = 50f
            style = Paint.Style.FILL
        }
        canvas.drawText("x", winWidth - 60f, coo.y - 40f, paint)
        canvas.drawText("y", coo.x - 40f, winHeight - 60f, paint)
        paint.apply {
            textSize = 30f
        }
        //正x轴文字
        for (i in 1 until ((winWidth - coo.x) / 50)) {
            paint.strokeWidth = 2f
            canvas.drawText("${100 * i}", coo.x - 20f + 100 * i, coo.y + 40f, paint)
            paint.strokeWidth = 5f
            canvas.drawLine(coo.x + 100f * i, coo.y.toFloat(), coo.x + 100f * i, coo.y - 10f, paint)
        }
        //负x轴文字
        for (i in 1 until (coo.x / 50)) {
            paint.strokeWidth = 2f
            canvas.drawText("${-100 * i}", coo.x - 20 - 100f * i, coo.y + 40f, paint)
            paint.strokeWidth = 5f
            canvas.drawLine(coo.x - 100f * i, coo.y.toFloat(), coo.x - 100f * i, coo.y - 10f, paint)
        }

        //正y轴文字
        for (i in 1 until (winHeight - coo.y) / 50) {
            paint.strokeWidth = 2f
            canvas.drawText("${100 * i}", coo.x + 20f, coo.y + 10 + 100f * i, paint)
            paint.strokeWidth = 5f
            canvas.drawLine(coo.x.toFloat(), coo.y + 100f * i, coo.x + 10f, coo.y + 100f * i, paint)
        }
        //负y轴文字
        for (i in 1 until coo.y / 50) {
            paint.strokeWidth = 2f
            canvas.drawText("${-100 * i}", coo.x + 20f, coo.y + 10 - 100f * i, paint)
            paint.strokeWidth = 5f
            canvas.drawLine(coo.x.toFloat(), coo.y - 100f * i, coo.x + 10f, coo.y - 100f * i, paint)
        }
    }

    /**
     * 绘制网格
     * @param canvas canvas
     */
    private fun drawGrid(canvas: Canvas) {
        paint.apply {
            color = Color.GRAY
            strokeWidth = 2f
            style = Paint.Style.STROKE
            pathEffect = dash
        }
        val path = Path()
        for (i in 0 until (winHeight / 50 + 1)) {
            path.moveTo(0f, 50f * i)
            path.lineTo(winWidth.toFloat(), 50f * i)
        }

        for (i in 0 until (winWidth / 50 + 1)) {
            path.moveTo(50f * i, 0f)
            path.lineTo(50f * i, winHeight.toFloat())
        }
        canvas.drawPath(path, paint)


    }


}